package org.fcs.framework.generator;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/2/23.
 */
public class DataInfo {

	/**
	 * 获取表信息
	 * 
	 * @param tableName
	 * @return
	 */
	public static Map<String, Object> readTableName(String tableName) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tableName", tableName);
		String className = Tools.getFieldName(tableName.substring(2));
		map.put("paramName", className);
		map.put("className", Tools.firstUpperCase(className));
		return map;
	}

	/**
	 * 获取表的列信息
	 *
	 * @param tableName
	 *            表名
	 * @return List<Map<String, Object>> 列信息列表
	 */
	public static List<Map<String, Object>> readColData(String tableName) {
		Connection conn = DbUtils.getConnection();
		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();

		try {
			DatabaseMetaData dbmd = conn.getMetaData();

			ResultSet rs = dbmd.getColumns(null, null, tableName, null);
			Map<String, Object> map = null;
			while (rs.next()) {
				map = new HashMap<String, Object>();
				String name = Tools.getFieldName(rs.getString("COLUMN_NAME"));
				map.put("name", name);
				map.put("column", rs.getString("COLUMN_NAME"));
				map.put("property", name);
				map.put("methodName", Tools.firstUpperCase(name));
				// map.put("type", Tools.getJavaType((Integer)
				// rs.getInt("DATA_TYPE")));
				map.put("type", JavaTypeNameTranslator.getJavaTypeName(rs
						.getInt("DATA_TYPE")));
				map.put("jdbcType", JdbcTypeNameTranslator.getJdbcTypeName(rs
						.getInt("DATA_TYPE")));
				map.put("remarks", rs.getString("REMARKS"));
				dataList.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.closeConnection(conn);
		}

		return dataList;
	}

	/**
	 * 读取所有的table，当types为null时，只返回表 types数组中可选的值为："TABLE"、"VIEW"、"SYSTEM TABLE"，
	 * "GLOBAL TEMPORARY"，"LOCAL  TEMPORARY"，"ALIAS"，"SYSNONYM"
	 * 
	 * @param type
	 * @return 返回所有table列表
	 */
	public static List<String> readTables(String type) {
		if (type == null)
			type = "TABLE";
		Connection conn = DbUtils.getConnection();
		List<String> tblList = new ArrayList<String>();
		try {
			DatabaseMetaData dbmd = conn.getMetaData();
			ResultSet rs = dbmd.getTables(null, null, null,
					new String[] { type });
			while (rs.next()) {
				tblList.add(rs.getString("TABLE_NAME"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbUtils.closeConnection(conn);
		}
		return tblList;
	}

	public static List<String[]> readTables() {
		Connection conn = DbUtils.getConnection();
		List<String[]> tblList = new ArrayList<String[]>();
		try {
			DatabaseMetaData dbmd = conn.getMetaData();
			ResultSet rs = dbmd.getTables(null, null, null, null);
			while (rs.next()) {
				tblList.add(new String[] { rs.getString("TABLE_TYPE"),
						rs.getString("TABLE_NAME") });
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbUtils.closeConnection(conn);
		}
		return tblList;
	}

	public static void main(String[] args) {
//		List list = DataInfo.readColData("t_test");
//		System.out.println(list);
	}
}
